// Decompiled by Jad v1.5.8f. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ResumenDAO.java

package org.fiel.admin.dao;

import java.io.PrintStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.fiel.admin.struts.forms.ResumenForm;
import org.fiel.domain.ItemResumenSelector;
import org.fiel.domain.Resumen;

public class ResumenDAO
{

    public ResumenDAO()
        throws Exception
    {
        conn = null;
        try
        {
            Context ctx = new InitialContext();
            DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/FielDB");
            conn = ds.getConnection();
        }
        catch(Exception e)
        {
            System.err.println("ResumenDAO() Error:\n" + e.getMessage());
            throw new Exception("ResumenDAO() Error:\n" + e.getMessage());
        }
    }

    public void createResumen(ResumenForm resumen)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("INSERT INTO resumen (Titulo, TituloE, Descripcion, DescripcionE, valor, publicacion) VALUES(?,?,?,?,?,?)");
            pstmt.setString(1, resumen.getTitulo());
            pstmt.setString(2, resumen.getTituloE());
            pstmt.setString(3, resumen.getDescripcion());
            pstmt.setString(4, resumen.getDescripcionE());
            pstmt.setString(5, resumen.getValor());
            pstmt.setString(6, resumen.getPublicacion());
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("ResumenDAO.createResumen() Error:\n" + e.getMessage());
            throw new Exception("ResumenDAO.createResumen() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    public List retrieveResumenes()
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        List resumenes = new ArrayList();
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM resumen ORDER BY Indeint");
            Resumen resumen;
            for(rset = pstmt.executeQuery(); rset.next(); resumenes.add(resumen))
            {
                resumen = new Resumen();
                resumen.setTitulo(rset.getString("Titulo"));
                resumen.setTituloE(rset.getString("TituloE"));
                resumen.setDescripcion(rset.getString("Descripcion"));
                resumen.setDescripcionE(rset.getString("DescripcionE"));
                resumen.setValor(rset.getString("valor"));
                resumen.setPublicacion(rset.getString("publicacion"));
                resumen.setIndeint(Integer.valueOf(rset.getString("Indeint")));
            }

        }
        catch(Exception e)
        {
            System.err.println("ResumenDAO.retrieveResumenes() Error:\n" + e.getMessage());
            throw new Exception("ResumenDAO.retrieveResumenes() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return resumenes;
    }

    public Resumen retrieveResumen(String id)
        throws Exception
    {
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        Resumen resumen = null;
        try
        {
            pstmt = conn.prepareStatement("SELECT * FROM resumen WHERE Indeint = ?");
            pstmt.setString(1, id);
            rset = pstmt.executeQuery();
            if(rset.next())
            {
                resumen = new Resumen();
                resumen.setTitulo(rset.getString("Titulo"));
                resumen.setTituloE(rset.getString("TituloE"));
                resumen.setDescripcion(rset.getString("Descripcion"));
                resumen.setDescripcionE(rset.getString("DescripcionE"));
                resumen.setValor(rset.getString("valor"));
                resumen.setPublicacion(rset.getString("publicacion"));
                resumen.setIndeint(Integer.valueOf(rset.getString("Indeint")));
            }
        }
        catch(Exception e)
        {
            System.err.println("ResumenDAO.retrieveResumen() Error:\n" + e.getMessage());
            throw new Exception("ResumenDAO.retrieveResumen() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset != null)
                    rset.close();
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return resumen;
    }

    public List retrieveItemResumenSelector()
        throws Exception
    {
        PreparedStatement pstmt1 = null;
        PreparedStatement pstmt2 = null;
        ResultSet rset1 = null;
        ResultSet rset2 = null;
        List itemResumenSelector = new ArrayList();
        ItemResumenSelector resumenSelect = null;
        try
        {
            pstmt1 = conn.prepareStatement("select a.idenint,a.titulo,b.descripcion as tipo, c.dessubtipo as subtipo from informacion a, tipos b, subtipos c where (a.identipo = 13) and b.identipo= a.identipo and c.identipo = b.identipo and c.idensubtipo = a.idensubtipo");
            for(rset1 = pstmt1.executeQuery(); rset1.next(); itemResumenSelector.add(resumenSelect))
            {
                resumenSelect = new ItemResumenSelector();
                resumenSelect.setTitulo(rset1.getString("titulo"));
                resumenSelect.setTipo(rset1.getString("tipo"));
                resumenSelect.setSubtipo(rset1.getString("subtipo"));
                resumenSelect.setIdenint(rset1.getString("idenint"));
            }

            pstmt2 = conn.prepareStatement("select a.idenint,a.titulo,b.descripcion as tipo, \"\" as subtipo from informacion a, tipos b where (a.identipo = 11) and b.identipo= a.identipo order by a.fechaE desc limit 1");
            for(rset2 = pstmt2.executeQuery(); rset2.next(); itemResumenSelector.add(resumenSelect))
            {
                resumenSelect = new ItemResumenSelector();
                resumenSelect.setTitulo(rset2.getString("titulo"));
                resumenSelect.setTipo(rset2.getString("tipo"));
                resumenSelect.setSubtipo(rset2.getString("subtipo"));
                resumenSelect.setIdenint(rset2.getString("idenint"));
            }

        }
        catch(Exception e)
        {
            System.err.println("ResumenDAO.retrieveItemResumenSelector() Error:\n" + e.getMessage());
            throw new Exception("ResumenDAO.retrieveItemResumenSelector() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(rset1 != null)
                    rset1.close();
                if(pstmt1 != null)
                    pstmt1.close();
                if(rset2 != null)
                    rset2.close();
                if(pstmt2 != null)
                    pstmt2.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
        return itemResumenSelector;
    }

    public void updateResumen(ResumenForm resumen)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("UPDATE resumen SET Titulo = ?, TituloE = ?, Descripcion = ?, DescripcionE = ?, valor = ?, publicacion = ? WHERE Indeint = ?");
            pstmt.setString(1, resumen.getTitulo());
            pstmt.setString(2, resumen.getTituloE());
            pstmt.setString(3, resumen.getDescripcion());
            pstmt.setString(4, resumen.getDescripcionE());
            pstmt.setString(5, resumen.getValor());
            pstmt.setString(6, resumen.getPublicacion());
            pstmt.setString(7, resumen.getId());
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("ResumenDAO.updateResumen() Error:\n" + e.getMessage());
            throw new Exception("ResumenDAO.updateResumen() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    public void deleteResumen(String id)
        throws Exception
    {
        PreparedStatement pstmt = null;
        try
        {
            conn.setAutoCommit(false);
            pstmt = conn.prepareStatement("DELETE FROM resumen WHERE Indeint = ?");
            pstmt.setString(1, id);
            pstmt.executeUpdate();
            conn.commit();
            conn.setAutoCommit(true);
        }
        catch(Exception e)
        {
            conn.rollback();
            System.err.println("ResumenDAO.deleteResumen() Error:\n" + e.getMessage());
            throw new Exception("ResumenDAO.deleteResumen() Error:\n" + e.getMessage());
        }
        finally
        {
            try
            {
                if(pstmt != null)
                    pstmt.close();
                if(conn != null)
                    conn.close();
            }
            catch(Exception ex) { }
        }
    }

    private Connection conn;
}
